package com.spzx.payment.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.context.SecurityContextHolder;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.rabbit.constant.MqConst;
import com.spzx.common.rabbit.service.RabbitService;
import com.spzx.order.api.RemoteOrderInfoService;
import com.spzx.order.api.domain.OrderInfo;
import com.spzx.order.api.domain.OrderItem;
import com.spzx.payment.domain.PaymentInfo;
import com.spzx.payment.mapper.PaymentInfoMapper;
import com.spzx.payment.service.IPaymentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 付款信息Service业务层处理
 */
@Service
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements IPaymentInfoService {
    @Autowired
    private PaymentInfoMapper paymentInfoMapper;

    @Autowired
    private RemoteOrderInfoService remoteOrderInfoService;

    @Autowired
    private RabbitService rabbitService;

    @Override
    public PaymentInfo savePayment(String orderNo) {
        Long userId = SecurityContextHolder.getUserId();
        //去重处理
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(new LambdaQueryWrapper<PaymentInfo>().eq(PaymentInfo::getOrderNo, orderNo));
        if (paymentInfo == null) {
            R<OrderInfo> byOrderNo = remoteOrderInfoService.getByOrderNo(orderNo, SecurityConstants.INNER);
            if (R.FAIL==byOrderNo.getCode()) {
                throw new ServiceException(byOrderNo.getMsg());
            }
            OrderInfo orderInfo = byOrderNo.getData();
            paymentInfo = new PaymentInfo();
            paymentInfo.setUserId(userId);
            paymentInfo.setOrderNo(orderNo);
            paymentInfo.setPayType(2);
            paymentInfo.setAmount(orderInfo.getTotalAmount());
            List<OrderItem> orderItemList = orderInfo.getOrderItemList();
            StringBuilder stringBuilder = new StringBuilder();
            for (OrderItem orderItem : orderItemList) {
                stringBuilder.append(orderItem.getSkuName()+";");
            }
            paymentInfo.setContent(stringBuilder.toString());
            paymentInfo.setPaymentStatus("0");
            paymentInfoMapper.insert(paymentInfo);
        }
        return paymentInfo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updatePaymentStatus(Map<String, String> paramMap, Integer payType) {
        PaymentInfo paymentInfo = paymentInfoMapper.
                selectOne(new LambdaQueryWrapper<PaymentInfo>().
                        eq(PaymentInfo::getOrderNo, paramMap.get("out_trade_no")));
        if ("1".equals(paymentInfo.getPaymentStatus())) {
        }
        paymentInfo.setPayType(payType);
        paymentInfo.setPaymentStatus("1");
        paymentInfo.setTradeNo(paramMap.get("trade_no"));//支付宝和用户之间扣款产生交易流水号
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setCallbackContent(JSON.toJSONString(paramMap));
        paymentInfoMapper.updateById(paymentInfo);
        //基于MQ通知订单系统，修改订单状态
        rabbitService.sendMessage(MqConst.EXCHANGE_PAYMENT_PAY,MqConst.ROUTING_PAYMENT_PAY,paymentInfo.getOrderNo());
    }
}